// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import store from "./store/"
import App from './App'
import 'sweetalert2/dist/sweetalert2.min.css'
import router from './router'
import wechat from 'weixin-js-sdk'
import {
http,
imgPath
} from "./plugins/http"
Vue.prototype.$wx = wechat;
Vue.config.productionTip = false;
Vue.use(require('vue-wechat-title'))
window.http = http;
window.imgPath = imgPath;
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  created() {
    //对地址栏的url进行封装(在#/前加上?为了解决微信支付中无法识别子目录的问题)
    let url = location.href;
    let param = this.queryJson();
   /* alert("url:"+url);
    alert("search:"+location.search);
    alert("param:"+JSON.stringify(param));*/
    if(url.indexOf("/#/")>-1){
      window.location.replace(url.replace("\/#\/","\/?#\/"))
    }
    /*alert("url:"+url);*/
    if(param.code){
      http.post("/getOAuth2UserInfo.do",{code:param.code})
        .then(data=>{
          this.$store.commit('updateUserInfo',data.datum);
        })
    }
    //jssdk注册
    this.createJsapiSignature();

  },
  methods:{
    queryJson:function(){
      let url = location.search; //获取url中"?"符后的字串
      let theRequest = new Object();
      if (url.indexOf("?") != -1) {
        let str = url.substr(1);
        let strs = str.split("&");
        for(let i = 0; i < strs.length; i ++) {
          theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
      }
      return theRequest;
    },
    createJsapiSignature:function(){
      http.get('/createJsapiSignature.do',{params:{url:location.href}})
        .then(data => {
          this.$wx.config({
            debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
            appId: data.appId, // 必填，公众号的唯一标识
            timestamp: data.timestamp, // 必填，生成签名的时间戳
            nonceStr: data.nonceStr, // 必填，生成签名的随机串
            signature: data.signature,// 必填，签名，见附录1
            jsApiList: [
              'getNetworkType',
              'previewImage'
            ], // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
          });
        })
    },
  },
  template: '<App/>'
})

/*router.afterEach((router) =>{
  document.title =  router.meta.title || "";
  //解决ios下title无法显示的问题
  if (/ip(hone|od|ad)/i.test(navigator.userAgent)) {
    let iframe = document.createElement('iframe')
    iframe.src = '/MP_verify_zxjwxCcP80t475ww.txt'
    iframe.style.display = 'none'
    iframe.onload = function () {
      setTimeout(function () {
        iframe.remove()
      }, 0)
      document.body.appendChild(iframe)
    }
  }
});*/
